﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using HMS.BL;
using HMS.Common;
using HMS.DL;

namespace HMS.PL.Forms
{
    public partial class ConfigHierarchy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {

                string strOpern = String.IsNullOrEmpty(Request.QueryString["operation"]) ? "" : Request.QueryString["operation"].ToString();
                if (strOpern == null || strOpern == "")
                {
                    strOpern = Request.Form["oper"] + Request.QueryString["table"];
                }

                ConfigHierData objConfigData = new ConfigHierData();

                if (String.IsNullOrEmpty(Request.QueryString["reconstruct"]) == false)
                {
                    return;
                }

                switch (strOpern)
                {

                    case "bind_version"://done
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"].ToString());
                        Response.Write(objConfigData.GetVersions());
                        break;
                    case "get_HierarchyColumns":
                        Response.Write(objConfigData.GetHierColumns());
                        break;
                    case "get_HierarchyData":
                        objConfigData.sidx = String.IsNullOrEmpty(Request.Params["sidx"]) ? "" : Convert.ToString(Request.Params["sidx"].ToString());
                        objConfigData.sord = String.IsNullOrEmpty(Request.Params["sord"]) ? "desc" : Convert.ToString(Request.Params["sord"].ToString());
                        objConfigData.rows = String.IsNullOrEmpty(Request.Params["rows"]) ? 10 : Convert.ToInt32(Request.Params["rows"].ToString());
                        objConfigData.page = String.IsNullOrEmpty(Request.Params["page"]) ? 1 : Convert.ToInt32(Request.Params["page"].ToString());
                        objConfigData._search = String.IsNullOrEmpty(Request.Params["_search"]) ? false : Convert.ToBoolean(Request.Params["_search"]);
                        objConfigData.query = String.IsNullOrEmpty(Request.Params["searchQuery"]) ? "" : Convert.ToString(Request.Params["searchQuery"]);
                        Response.Write(objConfigData.GetHierarchydata());
                        break;
                    case "get_VersionColumns":
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        Response.Write(objConfigData.GetVerColumns());
                        break;
                    case "get_VersionData":
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.sidx = String.IsNullOrEmpty(Request.Params["sidx"]) ? "" : Convert.ToString(Request.Params["sidx"].ToString());
                        objConfigData.sord = String.IsNullOrEmpty(Request.Params["sord"]) ? "desc" : Convert.ToString(Request.Params["sord"].ToString());
                        objConfigData.rows = String.IsNullOrEmpty(Request.Params["rows"]) ? 10 : Convert.ToInt32(Request.Params["rows"].ToString());
                        objConfigData.page = String.IsNullOrEmpty(Request.Params["page"]) ? 1 : Convert.ToInt32(Request.Params["page"].ToString());
                        objConfigData._search = String.IsNullOrEmpty(Request.Params["_search"]) ? false : Convert.ToBoolean(Request.Params["_search"]);
                        objConfigData.query = String.IsNullOrEmpty(Request.Params["searchQuery"]) ? "" : Convert.ToString(Request.Params["searchQuery"]);
                        Response.Write(objConfigData.GetVersiondata());
                        break;
                    case "get_children"://done 
                        objConfigData.ParentId = String.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"].ToString());
                        objConfigData.IsRoot = String.IsNullOrEmpty(Request.QueryString["root_flag"]) ? false : Convert.ToBoolean(Request.QueryString["root_flag"].ToString());
                        objConfigData.TableName = Request.QueryString["table_name"].ToString();
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.QueryString["vers_id"]) ? 0 : Convert.ToInt32(Request.QueryString["vers_id"].ToString());
                        //string strDate = Request.QueryString["txtdate"];
                        //objConfigData.txtDate = Functions.GetDate(Request.QueryString["txtdate"]);
                        Response.Write(objConfigData.GetChildren());
                        break;
                    case "create_node": //done                
                        objConfigData.TableName = Request.QueryString["table_name"].ToString();
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.QueryString["vers_id"]) ? 0 : Convert.ToInt32(Request.QueryString["vers_id"].ToString());
                        objConfigData.NodeTypeId = String.IsNullOrEmpty(Request.QueryString["type_id"]) ? 0 : Convert.ToInt32(Request.QueryString["type_id"].ToString());
                        objConfigData.ParentId = String.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"].ToString());
                        objConfigData.NewNodeTypeName = Request.QueryString["new_Nodename"].ToString();
                        objConfigData.isOwnParent = Convert.ToBoolean(Request.QueryString["isOwnParent"]);
                        //objConfigData.NewNodeTypeId = Request.QueryString["newnode_typeid"].ToString();
                        Response.Write(objConfigData.CreateNodeType());
                        break;
                    case "rename_nodetype"://done
                        objConfigData.NodeTypeId = String.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"].ToString());
                        objConfigData.NewNodeTypeName = Request.QueryString["new_name"].ToString();
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.QueryString["vers_id"]) ? 0 : Convert.ToInt32(Request.QueryString["vers_id"].ToString());
                        Response.Write(objConfigData.UpdateNodeTypeName());
                        break;
                    case "remove_nodetype"://done
                        objConfigData.NodeTypeId = String.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"].ToString());
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.QueryString["vers_id"]) ? 0 : Convert.ToInt32(Request.QueryString["vers_id"].ToString());
                        Response.Write(objConfigData.RemoveNodeType());
                        break;
                    case "get_AttrColumns":
                        // objConfigData.NodeTypeId = String.IsNullOrEmpty(Request.QueryString["node_type_id"]) ? 0 : Convert.ToInt32(Request.QueryString["node_type_id"].ToString());
                        Response.Write(objConfigData.GetAttrColumns());
                        break;
                    case "get_AttrData":
                        objConfigData.NodeTypeId = String.IsNullOrEmpty(Request.QueryString["node_type_id"]) ? 0 : Convert.ToInt32(Request.QueryString["node_type_id"].ToString());
                        objConfigData.sidx = String.IsNullOrEmpty(Request.Params["sidx"]) ? "" : Convert.ToString(Request.Params["sidx"].ToString());
                        objConfigData.sord = String.IsNullOrEmpty(Request.Params["sord"]) ? "desc" : Convert.ToString(Request.Params["sord"].ToString());
                        objConfigData.rows = String.IsNullOrEmpty(Request.Params["rows"]) ? 10 : Convert.ToInt32(Request.Params["rows"].ToString());
                        objConfigData.page = String.IsNullOrEmpty(Request.Params["page"]) ? 1 : Convert.ToInt32(Request.Params["page"].ToString());
                        objConfigData._search = String.IsNullOrEmpty(Request.Params["_search"]) ? false : Convert.ToBoolean(Request.Params["_search"]);
                        objConfigData.query = String.IsNullOrEmpty(Request.Params["searchQuery"]) ? "" : Convert.ToString(Request.Params["searchQuery"]);
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.QueryString["vers_id"]) ? 0 : Convert.ToInt32(Request.QueryString["vers_id"].ToString());
                        Response.Write(objConfigData.GetAttrdata());
                        break;
                    case "addX_NODE_TYPE_ATTR":
                        objConfigData.NodeTypeId = String.IsNullOrEmpty(Request.QueryString["node_type_id"]) ? 0 : Convert.ToInt32(Request.QueryString["node_type_id"].ToString());
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.QueryString["vers_id"]) ? 0 : Convert.ToInt32(Request.QueryString["vers_id"].ToString());
                        objConfigData.allkeys = Request.Form;
                        Response.Write(objConfigData.AddNodeAttr());
                        break;
                    case "delX_NODE_TYPE_ATTR":
                       // objConfigData.attr_id = String.IsNullOrEmpty(Request.Params["id"]) ? 0 : Convert.ToInt32(Request.Params["id"].ToString());
                        objConfigData.allkeys = Request.Form;
                        Response.Write(objConfigData.DelNodeAttr());
                        break;
                    case "editX_NODE_TYPE_ATTR":
                        objConfigData.allkeys = Request.Form;
                        Response.Write(objConfigData.EditNodeAttr());
                        break;       
                    case "delX_HIERARCHIES":
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.Params["id"]) ? 0 : Convert.ToInt32(Request.Params["id"].ToString());
                        Response.Write(objConfigData.delX_HIERARCHIES());
                        //Response.Write("true");
                        break;
                    case "editX_HIERARCHIES":
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.Form["id"]) ? 0 : Convert.ToInt32(Request.Form["id"].ToString());
                        objConfigData.allkeys = Request.Form;
                        Response.Write(objConfigData.EditHierarchy());
                        break;
                    case "addX_HIERARCHIES":
                        objConfigData.allkeys = Request.Form;
                        Response.Write(objConfigData.AddHierarchy());
                        break;
                    case "delX_HIER_VERSIONS":
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.Params["id"]) ? 0 : Convert.ToInt32(Request.Params["id"].ToString());
                        Response.Write(objConfigData.delVersion());
                        //Response.Write("true");
                        break;
                    case "editX_HIER_VERSIONS":
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.Params["id"]) ? 0 : Convert.ToInt32(Request.Params["id"].ToString());
                        objConfigData.allkeys = Request.Form;
                        Response.Write(objConfigData.EditVersion());
                        break;
                    case "addX_HIER_VERSIONS":
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.allkeys = Request.Form;
                        Response.Write(objConfigData.AddVersion());
                        break;
                    case "publishHierarchy":
                        objConfigData.HierarchyId = String.IsNullOrEmpty(Request.QueryString["hier_id"]) ? 0 : Convert.ToInt32(Request.QueryString["hier_id"].ToString());
                        objConfigData.VersionId = String.IsNullOrEmpty(Request.QueryString["vers_id"]) ? 0 : Convert.ToInt32(Request.QueryString["vers_id"].ToString());
                        Response.Write(objConfigData.publishHierarchy());
                        break;                    
                    default:
                        //Response.Write(objConfigData.GetXMasterListdata());
                        break;
                }
            }
        }
    }
}